package cn.zhizihua.domain.query;

import cn.zhizihua.domain.po.Address;
import cn.zhizihua.domain.po.User;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import lombok.Data;
import lombok.ToString;

@Data
@ToString
public class AddressQuery extends BaseQuery{

    private String contactName;  //根据收货人姓名模糊查询
    private String contactTel; //收货人电话模糊查询
    private String address; //收货人地址模糊查询
    private String userId; //收货人id查询

    /*
        <sql id="conditionSql">
        select addressId, contactName, contactGender, contactTel, address, a.userId, u.username
        from address a left join user u on a.userId=u.userId
        <where>
            <if test="contactName!=null and contactName!=''">
                and contactName like concat('%',#{contactName},'%')
            </if>
            <if test="contactTel!=null and contactTel!=''">
                and contactTel like concat('%',#{contactTel},'%')
            </if>
            <if test="address!=null and address!=''">
                and address like concat('%',#{address},'%')
            </if>
            <if test="username!=null and username!=''">
                and username like concat('%',#{username},'%')
            </if>
        </where>
    </sql>
    */

    //多表查询  查询地址信息及其对应用户名称&电话
    @Override
    public Wrapper getQueryWrapper() {
        MPJLambdaWrapper<Address> queryWrapper = new MPJLambdaWrapper<Address>()
                .setAlias("a") //为address表设置别名
                .selectAll(Address.class, "a")//查询address表全部信息
                .selectAs(User::getUserName, "'user.userName'") //联查user表的userName  注意查询别名需要''(单引号包裹)
                .leftJoin(User.class, "u", User::getUserId, Address::getUserId) //左连接user表，并设置别名u，连接条件为 a.userId=u.userId
                .orderByDesc("addressId");//排序规则
        //搜索条件
        if (StringUtils.isNotEmpty(contactName)){
            queryWrapper.like("contactName",contactName);
        }
        if (StringUtils.isNotEmpty(contactTel)){
            queryWrapper.like("contactTel",contactTel);
        }
        if (StringUtils.isNotEmpty(address)){
            queryWrapper.like("address",address);
        }
        if (StringUtils.isNotEmpty(userId)){
            queryWrapper.like("userId",userId);
        }
        return queryWrapper;
    }
}
